#include "ToolManager.h"

#define TIME_BETWEEN_POSITIONS 5000.0/(double)CLOCKS_PER_SEC

ToolManager::ToolManager(char * filename, char * saveFile)
{
	leftClickDepressed = false;
	leftClickWasDepressed = false;
	rightClickDepressed = false;
	rightClickWasDepressed = false;
	saveKeyDepressed = false;
	saveKeyWasDepressed = false;
	
	dragVertex = NULL;
	textureManager = new TextureManager();
	this->saveFile = saveFile;
	this->textureFile = filename;
	if(textureManager)
	{
		if(!textureManager->LoadTexture(filename))
		{
			printf("Failed to load %s\n", filename);
		}
		else
		{
			textureIndex = textureManager->TextureIndex(filename);
		}
	}
	objectList = new ObjectList();
}
ToolManager::~ToolManager()
{
	if(objectList)
	{
		delete(objectList);
		objectList = NULL;
	}
}
	
void ToolManager::PrepareFrame()
{
	float currentTime = (float)clock()/(float)CLOCKS_PER_SEC;
	float elapsedTime = currentTime - lastThoughtTimestamp;

	if(elapsedTime >= TIME_BETWEEN_POSITIONS)
	{
		lastThoughtTimestamp = currentTime;	
	}
	else
	{
		return;
	}
	
	for(;elapsedTime >= TIME_BETWEEN_POSITIONS; elapsedTime -= TIME_BETWEEN_POSITIONS)
	{
		float thisTime = TIME_BETWEEN_POSITIONS;
		if(elapsedTime < TIME_BETWEEN_POSITIONS)
			thisTime = elapsedTime; 
	
		thisTime /= 2.0;

		HandleUserInput();
		//camera->Think(thisTime);
		//tool->Think(thisTime);
		mouse.Think();
	}
}
void ToolManager::Draw()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,1,0,1, -1, 1);	
	
	glColor3f(1,1,1);
	glMatrixMode(GL_MODELVIEW);

	if(objectList)
		objectList->Draw();
		
	glPushMatrix();
	glLoadIdentity();
	
	glColor3f(1,1,1);
	
	if(textureIndex > 0)
	{
		textureManager->EnableTexture(textureIndex);
		glEnable(GL_TEXTURE_2D);
		
		glBegin(GL_QUADS);
		glTexCoord2f(0,1);
		glVertex2f(0,1);
		
		glTexCoord2f(1,1);
		glVertex2f(1,1);
		
		glTexCoord2f(1,0);
		glVertex2f(1,0);
		
		glTexCoord2f(0,0);
		glVertex2f(0,0);
		glEnd();
	}
		
	
	glPopMatrix();	
}
void ToolManager::SetNormalUserInput(unsigned char key, int modifier, bool isDepressed)
{
	printf("key =%d\n",key);
	if(1)
	{
		if(modifier == GLUT_ACTIVE_CTRL)
		{
			printf("Active ctrl\n");
			switch(key)
			{
				case 19:  saveKeyDepressed = isDepressed;break;
			}	
		}
		else
		{
			
			switch(key)
			{
			}	
		}
	}
}
void ToolManager::SetUserInput(int key, int modifier, bool isDepressed)
{
	switch(key)
	{
	}
}
void ToolManager::SetUserInput(int key, bool isDepressed, float x, float y)
{
	switch(key)
	{
		case GLUT_RIGHT_BUTTON: rightClickDepressed = isDepressed; break;
		case GLUT_LEFT_BUTTON: leftClickDepressed = isDepressed; break;
	}
}
void ToolManager::SetPassiveMouse(float x, float y)
{
	mouse.SetPosition(x,y, Vector2D(1,1));
}	
void ToolManager::SetActiveMouse(float x, float y)
{
	mouse.SetPosition(x,y,Vector2D(1,1));
}

void ToolManager::HandleUserInput()
{
	
	
	if(rightClickDepressed && !rightClickWasDepressed)
	{
		
	}
	if(leftClickDepressed && !leftClickWasDepressed)
	{
		Vector2D mPoint = mouse.GetPosition();
		mPoint.x = (mPoint.x + 100)/2.0;
		mPoint.y = (mPoint.y + 100)/2.0;
		printf("Y COORD = %f\n", mPoint.y);
		LeftClick();
		
	}
	if(!leftClickDepressed && leftClickWasDepressed)
	{
		if(!dragVertex)
			printf("Drag vertex is NULL before\n");
		dragVertex = NULL;
	}
	if(leftClickDepressed && leftClickWasDepressed)
	{
		LeftDrag();
	}
	if(saveKeyDepressed && !saveKeyWasDepressed)
	{
		SaveData();	
	}
	
	leftClickWasDepressed = leftClickDepressed;
	rightClickWasDepressed = rightClickDepressed;
	saveKeyWasDepressed = saveKeyDepressed;
}
void ToolManager::LeftClick()
{
	if(!dragVertex)
		printf("Drag vertex is NULL before click\n");
	
	if(!objectList || !(dragVertex = objectList->GetHoverVertex(&mouse)))
	{
		Vertex * corner1 = new Vertex(mouse.GetPosition());
		Vertex * corner2 = new Vertex(mouse.GetPosition());
		dragVertex = corner2;
		Object * object = new Object(corner1, corner2);
		objectList->Add(object);	
	}
	if(!dragVertex)
		printf("Drag vertex is NULL\n");
}
void ToolManager::LeftDrag()
{
	if(dragVertex)
	{
		dragVertex->SetPosition(mouse.GetPosition());
	}
}

void ToolManager::SaveData()
{
	if(objectList)
	{
		char * tag = objectList->GetTags();
		printf("TAG: %s\n", tag);
		if(tag)
		{
			FILE * fptr = NULL;
			
			if(!(fptr=fopen(saveFile, "w")))
			{
				printf("Failed to open %s\n",saveFile);
				return;
			}
			fprintf(fptr,"<texture, %s>\n", textureFile);
			fprintf(fptr,"%s",tag);
			fclose(fptr);
			fptr = NULL;
		}
		
	}
	
}